﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using NHibernate;
using Common;
using Educational.Model.Entity;
using Educational.Model.EntityManager;

namespace Educational.Business
{
    public class SchoolRoleCategoryAdmin
    {
        #region AddSchoolRoleCategory添加权限分类
        /// <summary>
        /// 添加权限分类
        /// </summary>
        /// <param name="categoryName"></param>
        public static void AddSchoolRoleCategory(string categoryName)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                SchoolRoleCategory schoolRoleCategory = new SchoolRoleCategory();
                schoolRoleCategory.categoryName = categoryName;
                SchoolRoleCategoryManager.Add(session, schoolRoleCategory);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region GetsSchoolRoleCategory获取所有权限分类，关联查询所有权限
        /// <summary>
        /// 获取所有权限分类，关联查询所有权限
        /// </summary>
        /// <returns></returns>
        public static IList<Educational.VO.SchoolRoleCategory> GetsSchoolRoleCategory()
        {
            ISession session = null;
            ITransaction trans = null;
            IList<SchoolRoleCategory> entityData = null;
            IList<Educational.VO.SchoolRoleCategory> list = new List<Educational.VO.SchoolRoleCategory>();
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = SchoolRoleCategoryManager.Gets1(session);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            {
                Educational.VO.SchoolRoleCategory schoolRoleCategory = new Educational.VO.SchoolRoleCategory();
                schoolRoleCategory.categoryId = entityData[i].categoryId;
                schoolRoleCategory.categoryName = entityData[i].categoryName;
                schoolRoleCategory.schoolRoles = new List<Educational.VO.SchoolRole>();
                IEnumerator<SchoolRole> schoolRoles = entityData[i].schoolRoles.GetEnumerator();
                while (schoolRoles.MoveNext())
                {
                    Educational.VO.SchoolRole schoolRole = new Educational.VO.SchoolRole();
                    schoolRole.roleId = schoolRoles.Current.roleId;
                    schoolRole.roleName = schoolRoles.Current.roleName;
                    schoolRoleCategory.schoolRoles.Add(schoolRole);
                }
                list.Add(schoolRoleCategory);
            }
            return list;
        }
        #endregion
    }
}
